package org.dream.persistence;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.dream.domain.Dealrecord;
import org.dream.util.ReflectUtil;

public class DealrecordPersistenceImpl {

	private static final String dealRecordFilePath = "dealrecord.txt";
	private static final String SEPARATOR_FLAG = ",";

	private static final String PROPERTYSTRING = "happenDate,happenTime,businessName,stockCode,stockName"
			+ ",dealPrice,dealNum,dealAmount,stockBalance,commissionCharge,stampTax,transferCharge,otherCharge"
			+ ",happenCharge,fundsBalance,entrustCode,entrustPrice,entrustNum,stockholderCode"
			+ ",fundsAccount,moneyKind,dealGroup";

	public void saveDealRecords(List<Dealrecord> dealRecords) {
		File file = new File(dealRecordFilePath);
		FileInputStream fileInputStream = null;
		InputStreamReader inputStreamReader = null;
		BufferedReader bufferedReader = null;

		PrintWriter printWriter = null;

		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			// 将文件读入输入流
			fileInputStream = new FileInputStream(file);
			inputStreamReader = new InputStreamReader(fileInputStream);
			bufferedReader = new BufferedReader(inputStreamReader);
			StringBuffer buf = new StringBuffer();

			// 保存该文件原有的内容
			String temp = "";
			for (int j = 1; (temp = bufferedReader.readLine()) != null; j++) {
				buf = buf.append(temp);
				buf = buf.append(System.getProperty("line.separator"));
			}
			// 添加新的数据
			for (Dealrecord dealRecord : dealRecords) {
				String[] props = PROPERTYSTRING.split(SEPARATOR_FLAG);
				for (String prop : props) {
					buf.append(ReflectUtil.getFieldValue(dealRecord, prop)).append(SEPARATOR_FLAG);
				}
				buf.append(System.getProperty("line.separator"));

			}

			printWriter = new PrintWriter(file, "gbk");
			printWriter.write(buf.toString().toCharArray());
			printWriter.flush();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}

	}

	public List<Dealrecord> findAllDealRecord() {
		List<Dealrecord> dealRecords = new ArrayList<Dealrecord>();
		File file = new File(dealRecordFilePath);
		FileInputStream fileInputStream = null;
		InputStreamReader inputStreamReader = null;
		BufferedReader bufferedReader = null;

		PrintWriter printWriter = null;

		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			// 将文件读入输入流
			fileInputStream = new FileInputStream(file);
			inputStreamReader = new InputStreamReader(fileInputStream, "gbk");
			bufferedReader = new BufferedReader(inputStreamReader);

			// 保存该文件原有的内容
			String temp = "";
			for (int j = 1; (temp = bufferedReader.readLine()) != null; j++) {
				Dealrecord dealRecord = new Dealrecord();
				String[] propValues = temp.split(SEPARATOR_FLAG);
				String[] props = PROPERTYSTRING.split(SEPARATOR_FLAG);
				for (int i = 0; i < props.length; i++) {
					ReflectUtil.setFieldValueByString(dealRecord, props[i], (String) propValues[i]);
				}
				dealRecords.add(dealRecord);

			}
			// 添加新的数据

		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}
		return dealRecords;
	}
}
